#include "mainwindow.h"
#include "ui_mainwindow.h"

#include <QStandardPaths>
#include <QDebug>
#include <QSqlDatabase>
#include <QDir>
#include <QSqlQuery>
#include <QDateTime>
#include <QSqlRelationalTableModel>

MainWindow::MainWindow(QWidget *parent) :
    QMainWindow(parent),
    ui(new Ui::MainWindow)
{
    ui->setupUi(this);

    //init 数据库
    QString databaseName = "apesay.db";
    QString database_username = "asdbData";
    QString database_password = "asdbData20180205*()";
    QString writable = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    QString databasePath = writable+"/"+databaseName;
    qDebug()<<writable;
    QDir dir(writable);
    if(!dir.exists()){
        qDebug() << "mkpath writable.";
        dir.mkpath(writable);
    }

    QSqlDatabase database;
    if (QSqlDatabase::contains("qt_sql_default_connection"))
    {
        qDebug() << "connection exist.";
        database = QSqlDatabase::database("qt_sql_default_connection");
    }
    else
    {
        qDebug() << "connection not exist.";
        database = QSqlDatabase::addDatabase("QSQLITE");
        database.setDatabaseName(databasePath);
        database.setUserName(database_username);
        database.setPassword(database_password);
    }

    if (database.open())
    {
        qDebug() << "Success to connect database.";

    }


    //init 信息表
    QString userInfo = "addresstype";
    QSqlQuery query_table;
    query_table.exec("select count(*) from sqlite_master where type='table' and name='"+userInfo+"'");
    if(query_table.next())
    {
        int num = query_table.value(0).toInt();
        qDebug()<<QString("num:%1").arg(num);

        if(num==0)
        {
            qDebug() << "table does not exist";

            if(!query_table.exec("create table "+userInfo+" (id int primary key, description varchar(20))"))
            {
                qDebug() << "Error: Fail to create table.";
            }
            else
            {
                qDebug() << "Table created!";
            }
        }
    }


    //插入|更新数据
    int timeT = QDateTime::currentDateTime().toTime_t();
    QSqlQuery query;

    query.exec("insert into "+userInfo+" values(101, 'Home')");

    query.exec("update "+userInfo+" set description='"+QString::number(timeT)+"' where id=101");

    //查询数据
    if(query.exec("select id,description from "+userInfo+""))
    {
        qDebug() << "query Wang!";
        while(query.next())
        {
            int id = query.value(0).toInt();
            QString name = query.value(1).toString();
            qDebug()<<QString("id:%1    name:%2").arg(id).arg(name);
        }
    }


    //        //文件存储
    //        QString writable = QStandardPaths::writableLocation(QStandardPaths::AppDataLocation);
    //        QDir dir(writable);
    //        if(!dir.exists()){
    //            qDebug() << "mkpath writable.";
    //            dir.mkpath(writable);
    //        }

    //        QFile *tempFile = new QFile;
    //        QString fileName = QString(writable+"/file.js");
    //        if(tempFile->exists(fileName)){
    //            tempFile->remove();
    //        }
    //        //此时，路径下没有fileName文件，使用下面代码在当前路径下创建文件
    //        tempFile->setFileName(fileName);
    //        if(tempFile->open(QIODevice::WriteOnly|QIODevice::Text))
    //        {
    //            tempFile->write(resultStr);
    //        }else {
    //            qDebug()<<fileName+"打开失败";
    //        }
    //        tempFile->close();
    qDebug() << "finish!";
}

MainWindow::~MainWindow()
{
    delete ui;
}
